package com.wandian.ui.controls
{
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.display.StageAlign;
	import flash.display.StageQuality;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.KeyboardEvent;
	import flash.filters.GlowFilter;
	import flash.utils.Dictionary;
	import flash.utils.getDefinitionByName;
	import flash.utils.getQualifiedClassName;
	import flash.utils.getQualifiedSuperclassName;
	

	use namespace ui_internal

	public class UIManager
	{
		private var listenersAttached:Boolean=false;
		private var invalidateSizeQueue:Array=[]
		private var invalidateDisplayQueue:Array=[]
		private var classStyleDictionary:Dictionary=new Dictionary(true);
		private static var _instance:UIManager;
		private static var initialized:Boolean;
		private var _stage:Stage
		private static var focusFilter:GlowFilter=new GlowFilter()
		private var focusUI:UIComponent

		public function UIManager(stage:Stage)
		{
			_stage=stage;
			_stage.stageFocusRect=false;
			_stage.quality=StageQuality.BEST; //指定极高呈现品质
			//_stage.align=StageAlign.TOP_LEFT; //指定舞台靠左上角对齐
	_stage.scaleMode=StageScaleMode.NO_SCALE; //指定应用程序的大小是固定的,即使在更改播放器窗口大小时，它仍然保持不变
			//stage.showDefaultContextMenu=false; //隐藏上下文菜单中的默认项。
			_stage.frameRate=30;
			var _className:String=getQualifiedClassName(UIComponent)
			classStyleDictionary[_className]=new GlobalStyleDeclaration();
			_stage.addEventListener(FocusEvent.KEY_FOCUS_CHANGE, tabHandler)
		}

		private function tabHandler(e:FocusEvent):void
		{
			if (e.keyCode != 9)
			{
				return
			}
			var ui:UIComponent=e.relatedObject as UIComponent
			if (focusUI != null)
			{
				focusUI.filters=[]
				focusUI=null
			}
			if (ui != null)
			{

				focusUI=ui
				focusUI.filters=[focusFilter]
			}
			else
			{
				focusUI=null
			}
		}

		public static function initialize(stage:Stage, manager:UIManager=null):void
		{
			if (initialized)
			{
				return;
			}

			initialized=true
			if (manager == null)
			{
				_instance=new UIManager(stage);
			}
			else
			{
				_instance=manager;
			}
		}

		public static function getInstance():UIManager
		{
			return _instance;
		}

		public function getStage():Stage
		{
			return _stage;
		}

		public function getClassStyleDeclaration(className:String, update:Boolean=true):ClassStyleDeclaration
		{
			var csd:ClassStyleDeclaration=classStyleDictionary[className];
			if (csd == null)
			{
				var _class:Object=getDefinitionByName(className);
				var parentClassName:String=getQualifiedSuperclassName(_class)
				var parent:ClassStyleDeclaration=getClassStyleDeclaration(parentClassName)
				csd=new ClassStyleDeclaration(className, parent);
				parent.addChild(csd);
				classStyleDictionary[className]=csd;
				if (update)
				{
					csd.fixParentStylesRef();
				}
			}
			return csd;
		}


		ui_internal function invalidateSize(ui:UIComponent):void
		{
			if (!listenersAttached)
			{
				attachListeners()
			}
			invalidateSizeQueue.push(ui)
		}

		ui_internal function invalidateDisplay(ui:UIComponent):void
		{
			if (!listenersAttached)
			{
				attachListeners()
			}
			invalidateDisplayQueue.push(ui)
		}

		private function validateSize():void
		{
			for (var i:int=0; i < invalidateSizeQueue.length; i++)
			{
				var ui:UIComponent=invalidateSizeQueue[i]
				ui.validateSize();
			}
			invalidateSizeQueue=[]
		}

		private function validateDisplay():void
		{
			for (var i:int=0; i < invalidateDisplayQueue.length; i++)
			{
				var ui:UIComponent=invalidateDisplayQueue[i]
				ui.validateDisplay();
			}
			invalidateDisplayQueue=[]
		}

		private function attachListeners():void
		{
			//application.setFrameRate(100);
			_stage.addEventListener(Event.RENDER, onRender)
			_stage.invalidate()
		}

		private function onRender(e:Event):void
		{
			//application.setFrameRate(30);
			_stage.removeEventListener(Event.RENDER, onRender)
			validateAll();
		}

		private function validateAll():void
		{
			trace("LayoutManager Validate All")
			validateSize();
			validateDisplay();
		}

	}
}
